﻿#include <iostream>
#include "SQLiteDB.h"

int main() {
    // 1. 创建 SQLite 数据库对象，指定数据库文件路径
    SQLiteDB sqlite("H:\\DWORKPLACE\\CWork\\CSQLite\\test.db");

    // 2. 创建表 user，如果表已存在则不会重复创建
    if(sqlite.Execute("CREATE TABLE IF NOT EXISTS user(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")) {
        std::cout << "Table 'user' created successfully." << std::endl;
    } else {
        std::cerr << "Failed to create table." << std::endl;
        return -1; // 创建表失败，退出程序
    }

    // 3. 插入数据：插入用户 Bob
    if(sqlite.Execute("INSERT INTO user(name) VALUES('Bob')")) {
        std::cout << "Inserted 'Bob' into table." << std::endl;
    } else {
        std::cerr << "Failed to insert data." << std::endl;
        return -1; // 插入失败，退出程序
    }

    // 插入用户 Alice
    if(sqlite.Execute("INSERT INTO user(name) VALUES('Alice')")) {
        std::cout << "Inserted 'Alice' into table." << std::endl;
    } else {
        std::cerr << "Failed to insert data." << std::endl;
    }

    // 4. 查询数据：打印表中所有记录
    std::cout << "Querying table 'user':" << std::endl;
    sqlite.Query("SELECT * FROM user", [](void* data, int argc, char** argv, char** colName){
        // argc 表示每行字段数，argv 保存字段值，colName 保存字段名
        for(int i = 0; i < argc; i++) {
            std::cout << colName[i] << ": " << (argv[i] ? argv[i] : "NULL") << " ";
        }
        std::cout << std::endl; // 每行打印结束换行
        return 0;
    });

    // 5. 删除操作：删除 name 为 'Bob' 的记录
    if(sqlite.Execute("DELETE FROM user WHERE name='Bob'")) {
        std::cout << "Deleted user 'Bob' successfully." << std::endl;
    } else {
        std::cerr << "Failed to delete user 'Bob'." << std::endl;
    }

    // 6. 再次查询，确认删除结果
    std::cout << "Querying table 'user' after deletion:" << std::endl;
    sqlite.Query("SELECT * FROM user", [](void* data, int argc, char** argv, char** colName){
        for(int i = 0; i < argc; i++) {
            std::cout << colName[i] << ": " << (argv[i] ? argv[i] : "NULL") << " ";
        }
        std::cout << std::endl;
        return 0;
    });

    return 0; // 程序结束
}
